
/**
 * Copyright (c) 2007, Ashish Lijhara
 * All rights reserved.
 * clockHandler.java
 *
 */

package crazy.java.Aetas;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Calendar;
import javax.swing.JComponent;

/**
 *
 * @author Ashish (Java Crazy)
 */
class clockHandler extends JComponent {
    
    
    private static final double TWO_PI   = 2.0 * Math.PI;
    
    private static final int    UPDATE_INTERVAL = 60000;
    private Calendar now = Calendar.getInstance();
    private int diameter;
    private int centerX;
    private int centerY;
    private BufferedImage clockImage;
    int Width,Height;
    private String zone;
    private String clock;
    private javax.swing.Timer timer;
    public clockHandler(int w,int h,String timezone,String currentclock) {
        zone=timezone;
        clock=currentclock;
        setPreferredSize(new Dimension(w,h));
        Width=w-10;Height=h-10;
        timer = new javax.swing.Timer(UPDATE_INTERVAL, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                updateTime();
                repaint();
            }
        });
    }
    public void start() {
        timer.start();
    }
    
    private void updateTime() {
        
        now.setTimeInMillis(System.currentTimeMillis());
    }
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        int w = Width;
        int h = Height;
        diameter = ((w < h) ? w : h);
        centerX = diameter / 2;
        centerY = diameter / 2;
        if (clockImage == null
                || clockImage.getWidth() != w
                || clockImage.getHeight() != h) {
            clockImage = (BufferedImage)(this.createImage(w, h));
            Graphics2D g2a = clockImage.createGraphics();
            g2a.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
        }
        
        drawClockHands(g2);
    }
    private void drawClockHands(Graphics2D g2) {
        
        
        
        int hours   = now.get(Calendar.HOUR);
        int minutes = now.get(Calendar.MINUTE);
        int seconds = now.get(Calendar.SECOND);
        int millis  = now.get(Calendar.MILLISECOND);
        if(zone.equals("GMT+05:30")){
            
            if(clock.equals("america")){
                hours=hours-11;
                minutes=minutes-30;
            }
        } else{
            
            if(clock.equals("asia")){
                hours=hours+11;
                minutes=minutes+30;
            }
        }
        int handMin = diameter / 8;
        int handMax = diameter / 2;
        
        double fseconds = (seconds + (double)millis/1000) / 60.0;
        
        handMin = 0;
        handMax = diameter / 3;
        double fminutes = (minutes + fseconds) / 60.0;
        
        drawRadius(g2, fminutes, 0, handMax,"min");
        
        
        handMin = 0;
        handMax = diameter / 4;
        
        drawRadius(g2, (hours + fminutes) / 12.0, 0, handMax,"hr");
    }
    
    private void drawRadius(Graphics2D g2, double percent,
            int minRadius, int maxRadius,String s) {
        //... percent parameter is the fraction (0.0 - 1.0) of the way
        //    clockwise from 12.   Because the Graphics2D methods use radians
        //    counterclockwise from 3, a little conversion is necessary.
        //    It took a little experimentation to get this right.
        double radians = (0.5 - percent) * TWO_PI;
        double sine   = Math.sin(radians);
        double cosine = Math.cos(radians);
        
        int dxmin = centerX + (int)(minRadius * sine);
        int dymin = centerY + (int)(minRadius * cosine);
        
        int dxmax = centerX + (int)(maxRadius * sine);
        int dymax = centerY + (int)(maxRadius * cosine);
        if(s.equals("sec")){
            g2.setStroke(new BasicStroke(1));
            g2.setColor(new Color(1.0f,0.0f,0.0f,0.5f));
            g2.drawLine(dxmin, dymin, dxmax, dymax);
        } else{
            if(s.equals("min")){
                g2.setStroke(new BasicStroke(2));
                g2.setColor(new Color(1.0f,1.0f,1.0f,0.5f));
                g2.drawLine(dxmin, dymin, dxmax, dymax);
            } else{
                g2.setStroke(new BasicStroke(3));
                g2.setColor(new Color(1.0f,1.0f,1.0f,0.5f));
                g2.drawLine(dxmin, dymin, dxmax, dymax);
            }
        }
        
        
        
    }
} 
